package com.lmbx.shzh.controller;

import com.lmbx.shzh.domain.common.ResponseResult;
import com.lmbx.shzh.service.RainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: wpc
 * @Date: 2021/5/13
 * @Description: 提供现有接口
 */
@Slf4j
@Api(tags = "降雨接口模块", value = "降雨接口模块相关接口")
@RestController
@RequestMapping("api/v1/rain")
public class RainController {
    @Autowired
    private RainService service;

    /**
     * 获取1，3，6小时降雨数据
     * @param vCode
     * @param hour
     * @return
     */
    @ApiOperation("获取时段雨量数据")
    @GetMapping("/timeRain")
    public ResponseResult timeRain(
            @ApiParam(name = "vCode",value = "地区编码",example = "1301") @RequestParam("vCode") String vCode,
            @ApiParam(name = "hour",value = "时间范围",example = "1") @RequestParam("hour")  @Min(value = 1, message = "时间范围不合法，最小为1") @Max(value = 24, message = "时间范围不合法，最大为24") int hour){
        return service.rain(vCode, hour);
    }

    /**
     * 获取1小时实时数据
     * @param vCode
     * @param hour
     * @return
     */
    @ApiOperation("获取时段实时数据")
    @GetMapping("/ycRain")
    public ResponseResult ycRain(
            @ApiParam(name = "vCode",value = "地区编码",example = "1301") @RequestParam("vCode") String vCode,
            @ApiParam(name = "hour",value = "时间范围",example = "1") @RequestParam("hour")  @Min(value = 1, message = "时间范围不合法，最小为1") @Max(value = 24, message = "时间范围不合法，最大为24") int hour){
        return service.ycRain(vCode, hour);
    }

    /**
     * 获取雨量列表数据
     * @param vCode
     * @return
     */
    @ApiOperation("获取累积雨量列表数据")
    @PostMapping("/getRain")
    public ResponseResult getRain(
            @ApiParam(name = "vCode",value = "地区编码",example = "1301") @RequestParam("vCode") String vCode,
            @ApiParam(name = "bt",value = "开始时间",example = "2021-06-07 08:00:00") @RequestParam("bt") String bt,
            @ApiParam(name = "et",value = "结束时间",example = "2021-06-07 15:00:00") @RequestParam("et") String et){
        bt = formatDate(bt);
        et = formatDate(et);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = dateFormat.parse(bt);
            Date d2 = dateFormat.parse(et);
            return service.getRain(vCode, d1, d2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ResponseResult.error("日期格式错误");
    }
    @ApiOperation("获取河道列表数据")
    @PostMapping("/getRsvr")
    public ResponseResult getRsvr(@ApiParam(name = "vCode",value = "地区编码",example = "1301") @RequestParam("vCode") String vCode,
                                  @ApiParam(name = "bt",value = "开始时间",example = "2021-06-07 08:00:00") @RequestParam("bt") String bt,
                                  @ApiParam(name = "et",value = "结束时间",example = "2021-06-07 15:00:00") @RequestParam("et") String et){
        bt = formatDate(bt);
        et = formatDate(et);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = dateFormat.parse(bt);
            Date d2 = dateFormat.parse(et);
            return service.getRsvr(vCode, d1, d2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ResponseResult.error("日期格式错误");
    }
    @ApiOperation("获取水库列表数据")
    @PostMapping("/getRiver")
    public ResponseResult getRiver(@ApiParam(name = "vCode",value = "地区编码",example = "1301") @RequestParam("vCode") String vCode,
                                   @ApiParam(name = "bt",value = "开始时间",example = "2021-06-07 08:00:00") @RequestParam("bt") String bt,
                                   @ApiParam(name = "et",value = "结束时间",example = "2021-06-07 15:00:00") @RequestParam("et") String et) {
        bt = formatDate(bt);
        et = formatDate(et);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = dateFormat.parse(bt);
            Date d2 = dateFormat.parse(et);
            return service.getRiver(vCode, d1, d2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ResponseResult.error("日期格式错误");
    }
    @ApiOperation("获取预警列表数据")
    @PostMapping("/getWarn")
    public ResponseResult getWarn(@ApiParam(name = "vCode",value = "地区编码",example = "1301") @RequestParam("vCode") String vCode,
                                  @ApiParam(name = "bt",value = "开始时间",example = "2021-06-07 08:00:00") @RequestParam("bt") String bt,
                                  @ApiParam(name = "et",value = "结束时间",example = "2021-06-07 15:00:00") @RequestParam("et") String et) {
        bt = formatDate(bt);
        et = formatDate(et);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = dateFormat.parse(bt);
            Date d2 = dateFormat.parse(et);
            return service.getWarn(vCode, d1, d2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ResponseResult.error("日期格式错误");
    }
    @ApiOperation("获取河道洪峰")
    @GetMapping("/getRvfcch")
    public ResponseResult rvfcch(@ApiParam(name = "vCode",value = "地区编码",example = "1301") @RequestParam("vCode") String vCode,
                                 @ApiParam(name = "hour",value = "时间范围",example = "1") @RequestParam("hour")  @Min(value = 1, message = "时间范围不合法，最小为1") @Max(value = 24, message = "时间范围不合法，最大为24") int hour) {
        return service.rvfcch(vCode,hour);
    }

    @ApiOperation("雨情信息汇总描述")
    @PostMapping("/getRainStatus")
    public ResponseResult getRainStatus(
            @ApiParam(name = "bt",value = "开始时间",example = "2021-06-07 08:00:00") @RequestParam("bt") String bt,
            @ApiParam(name = "et",value = "结束时间",example = "2021-06-07 15:00:00") @RequestParam("et") String et){
        bt = formatDate(bt);
        et = formatDate(et);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = dateFormat.parse(bt);
            Date d2 = dateFormat.parse(et);
            int count = service.rainingCount(d1, d2);
            Map<String, String> addressMap = service.maxRainAddress(d1, d2);
            String ad1 = addressMap.get("stnm").trim();
            String ad2 = addressMap.get("adnm");
            String mm = addressMap.get("mm");
            String result = String.format("雨情：%s -- %s，全省共有雨量监测站【6592】个，当前共【%d】个站降雨，最大降雨站点%s<%s>为%smm。",
                    bt, et, count, ad2, ad1, mm);
            return ResponseResult.success(result);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ResponseResult.error("日期格式错误");
    }

    @ApiOperation("河道信息汇总描述")
    @PostMapping("/getRsvrStatus")
    public ResponseResult getRsvrStatus(
            @ApiParam(name = "bt",value = "开始时间",example = "2021-06-07 08:00:00") @RequestParam("bt") String bt,
            @ApiParam(name = "et",value = "结束时间",example = "2021-06-07 15:00:00") @RequestParam("et") String et){
        bt = formatDate(bt);
        et = formatDate(et);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = dateFormat.parse(bt);
            Date d2 = dateFormat.parse(et);
            int count = service.rsvrCountOverWarn(d1, d2);
            String result = String.format("河道：%s -- %s，全省河道站共有【1157】个，当前【%d】个站点水位在警戒水位以上。",
                    bt, et, count);
            return ResponseResult.success(result);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ResponseResult.error("日期格式错误");
    }

    @ApiOperation("水库信息汇总描述")
    @PostMapping("/getRiverStatus")
    public ResponseResult getRiverStatus(
            @ApiParam(name = "bt",value = "开始时间",example = "2021-06-07 08:00:00") @RequestParam("bt") String bt,
            @ApiParam(name = "et",value = "结束时间",example = "2021-06-07 15:00:00") @RequestParam("et") String et){
        bt = formatDate(bt);
        et = formatDate(et);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = dateFormat.parse(bt);
            Date d2 = dateFormat.parse(et);
            int count = service.riverCountOverWarn(d1, d2);
            String result = String.format("水库：%s -- %s，全省水库站共有【765】个，当前【%d】个站点水位在汛限水位以上。",
                    bt, et, count);
            return ResponseResult.success(result);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ResponseResult.error("日期格式错误");
    }

    @ApiOperation("山洪预警信息汇总描述")
    @PostMapping("/getWarnStatus")
    public ResponseResult getWarnStatus(
            @ApiParam(name = "bt",value = "开始时间",example = "2021-06-07 08:00:00") @RequestParam("bt") String bt,
            @ApiParam(name = "et",value = "结束时间",example = "2021-06-07 15:00:00") @RequestParam("et") String et){
        bt = formatDate(bt);
        et = formatDate(et);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = dateFormat.parse(bt);
            Date d2 = dateFormat.parse(et);
            int sumCount = service.shWarnCount(d1, d2, null, null);
            int movingCount = service.shWarnCount(d1, d2, RainService.GRADE_4, null);
            int readyCount = service.shWarnCount(d1, d2, RainService.GRADE_5, null);
            int closeCount = service.shWarnCount(d1, d2, null, RainService.STATUS_CLOSED);
            String result = String.format("预警：%s -- %s，全省共产生山洪预警%d个，其中立即转移%d个，准备转移%d个，已关闭%d个，未关闭%d个。",
                    bt, et, sumCount, movingCount, readyCount, closeCount, sumCount - closeCount);
            return ResponseResult.success(result);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ResponseResult.error("日期格式错误");
    }

    @ApiOperation("获取水库和雨量预警数据列表")
    @PostMapping("/getAlarmList")
    public ResponseResult getAlarmList(
            @ApiParam(name = "alarmId",value = "预警Id",example = "")
                @RequestParam(value = "alarmId", required = false) String alarmId,
            @ApiParam(name = "startTime",value = "开始时间",example = "2021-04-26 17:46:59")
                @RequestParam(value = "startTime", required = false) String startTime,
            @ApiParam(name = "endTime",value = "结束时间",example = "2021-04-26 19:34:59")
                @RequestParam(value = "endTime", required = false) String endTime){
        if (!TextUtils.isEmpty(startTime)) {
            startTime = formatDate(startTime);
        }
        if (!TextUtils.isEmpty(endTime)) {
            endTime = formatDate(endTime);
        }
        return service.getAlarmList(alarmId, startTime, endTime);
    }

    @ApiOperation("获取实时雨情信息")
    @PostMapping("/getRainById")
    public ResponseResult getRainInfo(
            @ApiParam(name = "stcd",value = "测站编码",example = "30358234")
            @RequestParam(value = "stcd", required = true) @NotNull String stcd,
            @ApiParam(name = "bt",value = "开始时间",example = "2021-04-26 17:46:59")
            @RequestParam(value = "bt", required = true) String bt,
            @ApiParam(name = "et",value = "结束时间",example = "2021-04-26 19:34:59")
            @RequestParam(value = "et", required = true) String et){
        if (!TextUtils.isEmpty(bt)) {
            bt = formatDate(bt);
        }
        if (!TextUtils.isEmpty(et)) {
            et = formatDate(et);
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = dateFormat.parse(bt);
            Date d2 = dateFormat.parse(et);
            return service.getRainInfo(stcd, d1, d2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ResponseResult.error("日期格式错误");
    }

    @ApiOperation("实时水库水情")
    @PostMapping("/getRsvrById")
    public ResponseResult getRsvrInfo(
            @ApiParam(name = "stcd",value = "测站编码",example = "30358234")
            @RequestParam(value = "stcd", required = true) @NotNull String stcd,
            @ApiParam(name = "bt",value = "开始时间",example = "2021-04-26 17:46:59")
            @RequestParam(value = "bt", required = true) String bt,
            @ApiParam(name = "et",value = "结束时间",example = "2021-04-26 19:34:59")
            @RequestParam(value = "et", required = true) String et){
        if (!TextUtils.isEmpty(bt)) {
            bt = formatDate(bt);
        }
        if (!TextUtils.isEmpty(et)) {
            et = formatDate(et);
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = dateFormat.parse(bt);
            Date d2 = dateFormat.parse(et);
            return service.getRsvrInfo(stcd, d1, d2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ResponseResult.error("日期格式错误");
    }

    @ApiOperation("实时河道水情")
    @PostMapping("/getRiverById")
    public ResponseResult getRiverById(
            @ApiParam(name = "stcd",value = "测站编码",example = "30358234")
            @RequestParam(value = "stcd", required = true) @NotNull String stcd,
            @ApiParam(name = "bt",value = "开始时间",example = "2021-04-26 17:46:59")
            @RequestParam(value = "bt", required = true) String bt,
            @ApiParam(name = "et",value = "结束时间",example = "2021-04-26 19:34:59")
            @RequestParam(value = "et", required = true) String et){
        if (!TextUtils.isEmpty(bt)) {
            bt = formatDate(bt);
        }
        if (!TextUtils.isEmpty(et)) {
            et = formatDate(et);
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = dateFormat.parse(bt);
            Date d2 = dateFormat.parse(et);
            return service.getRiverById(stcd, d1, d2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ResponseResult.error("日期格式错误");
    }

    @ApiOperation("预警记录 内容包括内部预警发送短信数量；外部预警发送短信数量；解除预警")
    @PostMapping("/getWarnById")
    public ResponseResult getWarnById(
            @ApiParam(name = "warnId",value = "预警ID",example = "0000B7FE-6799-402F-9573-C8DD22A3A954130627")
            @RequestParam(value = "warnId", required = true) @NotNull String warnId){
        int inner = service.getWarnById(warnId, "10");
        int outer = service.getWarnById(warnId, "20");
        Map<String, Integer> result = new HashMap<>();
        result.put("inner", inner);
        result.put("outer", outer);
        return ResponseResult.success(result);
    }

    /**
     * 处理日期的数据格式，返回规范格式
     * @return
     */
    private String formatDate(String date) {
        try {
            date = URLDecoder.decode(date, "UTF-8");
            date = date.replace("$nbsp;", " ");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return date;
    }


}
